#include <stdio.h>
#include <stdlib.h>
#include "func.h"

int lengthp=0;

link_node * creat_head(void)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	p->data=-1;
	p->next=NULL;

	return p;
}

int head_insert(link_node *H)
{
	int inputnum=1;
	while(1)
	{
		printf("plz input num (-1->end)\n");
		scanf("%d",&inputnum);
		if(inputnum ==-1)
		{
			break;
		}
		head_insert1(H,&inputnum);
		lengthp++;
	}

	return 0;
}

int head_insert1(link_node *H,int *inputnum)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	p->data=*inputnum;
	p->next=H->next;
	H->next=p;

	return 0;
}

int ifhuiwen(link_node *H)
{
	link_node *fast=H;
	link_node *slow=H;
	if(lengthp%2==1)
	{
		return -1;
	}
	while(fast->next!=NULL)
	{
		fast=fast->next;
		fast=fast->next;
		slow=slow->next;
	}
	link_node *slow1=slow;
	returnlink(slow);
	while(slow->next!=NULL)
	{
		slow=slow->next;
		H=H->next;
		if(slow->data!=H->data)
		{
			return -1;
		}
	}
	returnlink(slow1);

	return 0;
}

int returnlink(link_node *H)
{
	link_node *p=H->next;
	link_node *q=p->next;

	while(p->next!=NULL)
	{
		p->next=q->next;
		q->next=H->next;
		H->next=q;

		q=p->next;
	}
	return 0;
	
}

